﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBORpt = Texfina.BORpt.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;

namespace RH
{
   public partial class wfPerVacaDiasRpt : EditPage
   {
      private Font printFont;
      private int _indexRow = 0;
      private int _NumberPage = 0;

      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();

      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = FormEditAction.Edit;
         this.SetInitialTitle("Dias de vacaciones");
      }

      public override void LoadListControls()
      {
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
         SYBOQry.IEmpresa objBO = WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));
         EEmpPeriodo objE = new EEmpPeriodo();

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         DataTable dt = objBO.GetPeriodos(objE);

         ddlidPeriodo.Items.Clear();
         WebHelper.LoadListControl(ddlidPeriodo, dt, "idPeriodo", "idPeriodo", true);
         WebHelper.ListControlSetValue(ddlidPeriodo, DateTime.Now.Year.ToString());

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {

            case ConstMenuToolbarValue.BUTTON_PREVIEW:
               PreviewReport(e.Item.Value);
               break;
            case ConstMenuToolbarValue.BUTTON_EXPORT:
               ExportReport(e.Item.Value);
               break;
         }

      }

      public void PreviewReport(string key)
      {
         ReportDocument objRpt = new ReportDocument();

         DataSet ds = (DataSet)this .GetDataReport(key);

         DataTable dt = ds.Tables[0];

         string NomRep="";

         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No se encotraron Resultados", "");
            return;
         }

         if ((dt.Rows[0]["idTipoTrab"].ToString() != "999") && (dt.Rows[0]["idTipoTrab"].ToString() != "023"))
         {
            NomRep = "BoletaTrabajador";
            objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/BoletaTrabajador.rpt"));
         }
         else {

            if (dt.Rows[0]["idTipoTrab"].ToString() == "999")
            {
               NomRep = "BoletaFLJ";
               objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/BoletaFLJ.rpt"));
            }

            if (dt.Rows[0]["idTipoTrab"].ToString() == "023")
            {
               NomRep = "BoletaSENATI";
               objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/BoletaSENATI.rpt"));
            }
         }


         objRpt.SetDataSource(ds.Tables[0]);

         MemoryStream ms = new MemoryStream();

         Response.Clear();

         ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
         objRpt.Dispose();
         objRpt = null;

         Response.AddHeader("Content-Disposition", "attachment;filename=" + NomRep + ".pdf");
         Response.Buffer = true;
         Response.ContentType = "application/pdf";

         ms.WriteTo(Response.OutputStream);
         Response.Flush();
         Response.End();

      }

      public override void ExportReport(string key)
      {
         DataSet ds = (DataSet)this.GetDataReport(key);
         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No se encotraron Resultados", "");
            return;
         }
         ExcelHelper.ToExcel(ds, "Data.xls", Response);

      }

      public override object GetDataReport(string key)
      {

         EVacaDias objEDet = new EVacaDias();
         RHBORpt.IPlanilla objBODet = (RHBORpt.IPlanilla)WCFHelper.GetObject<RHBORpt.IPlanilla>(typeof(RHBORpt.Planilla));
         objEDet.IdPeriodo = ddlidPeriodo.SelectedItem.Value;
         objEDet.IdPlanilla = ddlidTipoTrab.SelectedValue.ToString();
         objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         //objEDet.IdForPago = this.ddlidTipoTrab.SelectedItem.Value;
                
         DataSet ds = new DataSet();

         DataTable dt = null;

         dt = objBODet.GetVacaDiasPrint(objEDet);
         ds.Tables.Add(dt);

         return ds;


      }

   }
}